/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.model.service;

import com.model.domain.ubicacion.Departamento;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


/**
 *
 * @author Rdelacruz
 */
public class DepartamentoService 
extends AbstractService
{
    
     public Departamento findByCodigo(Connection con, String codigo) throws SQLException {
        Statement st = null;
        try {                        
            st = con.createStatement();
            String sql = "select * from Dpto where CCDD='"+codigo+"'";
            ResultSet rs = st.executeQuery(sql);            
            while(rs.next()) {
                Departamento dep = new Departamento();
                dep.setCodigo(rs.getString("CCDD"));
                dep.setNombre(rs.getString("NombreDD"));
                return dep;
            }
        }
        finally {
            if(st!=null) st.close();
        }
        return null;
    }

    public List<Departamento> findAll(Connection con) throws SQLException {
        List<Departamento> lista = new ArrayList<Departamento>();
        Statement st = null;
        try {                        
            st = con.createStatement();
            String sql = "select * from Dpto";
            ResultSet rs = st.executeQuery(sql);            
            while(rs.next()) {
                Departamento dep = new Departamento();
                dep.setCodigo(rs.getString("CCDD"));
                dep.setNombre(rs.getString("NombreDD"));
                
                lista.add(dep);
            }
        }
        finally {
            if(st!=null) st.close();
        }
        return lista;
    }

    public List<Departamento> findByCodigos(Connection con, String[] codigos) throws SQLException {
        List<Departamento> lista = findAll(con);
        if(codigos==null) {
            return lista;
        }
        else {
            List<Departamento> lista1 = new ArrayList<Departamento>();
            for(Departamento obj:lista)  {
                for(String c : codigos) {
                    if(c.equals(obj.getCodigo())) {
                        lista1.add(obj);
                        break;
                    }
                }              
            }                
            return lista1;
        }
    }

    
    
}
